//
//  ACLoginManager.m
//  ACLiteAVUISDK_Example
//
//  Created by 冯琰琰 on 2021/4/12.
//  Copyright © 2021 fyanyan. All rights reserved.
//

#import "ACLoginManager.h"

@implementation ACLoginManager

+(void)requestVertifyCodeWithPhone:(NSString *)phone requestCallback:(nonnull RequestHandler)requestCallback{
    NSString *url = [HOSTURL stringByAppendingString:@"/api/v1/auth/sms"];
    NSDictionary *params = @{@"phone":phone,@"type":@"login"};
    [[ACNetWorkTool shareRequestManager] post:url params:params success:^(id  _Nonnull responseObject) {
        if (requestCallback) {
            requestCallback(0,@"successfully");
        }
    } failure:^(NSError * _Nonnull error) {
        if (requestCallback) {
            requestCallback( -1 , @"get vertifyCode failed");
        }
    }];
}
// IM 同步登录到IM中
+(void)requestLoginWithPhone:(NSString *)phone vertifyCode:(NSString *)vertifyCode requestCallback:(RequestHandler)requestCallback{
    NSString *url = [HOSTURL stringByAppendingString:@"/api/v1/auth/login"];
    NSDictionary *params = @{@"phone":phone,@"sms_code":vertifyCode};
    [[ACNetWorkTool shareRequestManager] post:url params:params success:^(id  _Nonnull responseObject) {
        // 返回的信息 是用户的
        DDLogInfo(@"%@",responseObject);
        ACUserModel *userModel = [ACUserModel mj_objectWithKeyValues:responseObject];
        [ACUserProfileManager shareInstance].userModel = userModel;
        if (requestCallback) {
            requestCallback(0,@"successfully");
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

+(void)requestUserSigRequestCallback:(void (^)(id responseObject))success{
    NSString *url = [HOSTURL stringByAppendingString:@"/api/v1/tencent/sign"];
    [[ACNetWorkTool shareRequestManager] get:url params:[NSDictionary dictionary] success:^(id  _Nonnull responseObject) {
        DDLogInfo(@"%@",responseObject);
        success(responseObject);
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

// IM 也需要同步更新
+(void)requestSetUserProfileWithNickname:(NSString *)nickname avatar:(NSString *)avatar requestCallback:(RequestHandler)requestCallback{
    NSString *url = [HOSTURL stringByAppendingString:@"/api/v1/auth/me"];
    NSDictionary *params = @{@"nickname":nickname,@"avatar":avatar};
    [[ACNetWorkTool shareRequestManager] put:url params:params success:^(id  _Nonnull responseObject) {
        DDLogInfo(@"%@",responseObject);
        if (requestCallback) {
            requestCallback(0,@"successfully");
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

+(void)requestUserProfileWithRequestCallback:(RequestHandler)requestCallback{
    NSString *url = [HOSTURL stringByAppendingString:@"/api/v1/auth/me"];
    NSDictionary *parames = [NSDictionary dictionary];
    [[ACNetWorkTool shareRequestManager] get:url params:parames success:^(id  _Nonnull responseObject) {
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//退出登录
+(void)requestLogoutWithRequestCallback:(RequestHandler)requestCallback{
    NSString *url = [HOSTURL stringByAppendingString:@"/api/v1/auth/logout"];
    [[ACNetWorkTool shareRequestManager] post:url params:[NSDictionary dictionary] success:^(id  _Nonnull responseObject) {
        // 返回的信息
        DDLogInfo(@"Logout  %@",responseObject);
        // IM 同步登出
        [TRTCLiveRoomIMAction logout:^(int code, NSString * _Nonnull message) {
            if (code == 0) {
                [[ACUserProfileManager shareInstance] removeUserCache];
                if (requestCallback) {
                    requestCallback(0,@"successfully");
                }
            }else{
                if (requestCallback) {
                    requestCallback(-1,@"failure");
                }
            }
        }];
    } failure:^(NSError * _Nonnull error) {
        DDLogInfo(@"Logout  %@",error);
        if (requestCallback) {
            requestCallback(-1,@"failure");
        }
    }];
}

@end
